package hsp.admin.app.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.system.po.LogPO;
import hsp.admin.app.system.service.ILogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/log")
public class LogController {

    private final ILogService logService;

    @Autowired
    public LogController(ILogService logService){
        this.logService = logService;
    }

    @GetMapping("{id}")
    public ApiResult<LogPO> getLog(@PathVariable("id") long id){
        LogPO systemLog = logService.getById(id);
        return ApiResult.success(systemLog);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<LogPO>> getLogPage(@PathVariable("page")int page, @PathVariable("size")int size, LogPO logPO){
        LambdaQueryWrapper<LogPO> wrapper = new LambdaQueryWrapper<>();
        if(logPO.getId() != null) wrapper.eq(LogPO::getId, logPO.getId());
        if(logPO.getBizId() != null) wrapper.eq(LogPO::getBizId, logPO.getBizId());
        if(StringUtils.isNotBlank(logPO.getBizName())) wrapper.eq(LogPO::getBizName, logPO.getBizName());
        if(StringUtils.isNotBlank(logPO.getType())) wrapper.eq(LogPO::getType, logPO.getType());
        wrapper.orderByDesc(LogPO::getCreateTime);
        Page<LogPO> logPage = logService.page(new Page<>(page,size),wrapper);
        return ApiResult.success(logPage);
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteLog(@PathVariable("id") long id){
        logService.removeById(id);
        return ApiResult.success();
    }

}
